package com.zry.fertilizer.presenter

import android.content.Intent
import android.util.Log
import android.view.View
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
import com.zry.fertilizer.R
import com.zry.fertilizer.adapter.PutStorageRecordAdapter
import com.zry.fertilizer.animation.RecyclerViewUtilKt
import com.zry.fertilizer.been.*
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.activity.PutStorageRecordInfoActivity
import com.zry.fertilizer.ui.activity.PutStorageRecordListActivity
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.OneYardUtils
import com.zry.fertilizer.utils.ToastUtil
import com.zry.fertilizer.view.ProductNamePopup
import com.zry.fertilizer.view.PutStatePopup
import kotlinx.android.synthetic.main.activity_put_storage_record_list.*

class PutStorageRecordListPresenter(private val activity: PutStorageRecordListActivity) : ShowUserView {
    private var productNameSelect: ProductNameBean? = null
    private var putStateSelect: PutState? = null
    private var putStorageRecordAdapter: PutStorageRecordAdapter? = null


    fun putStorageHandlerClick(view: View) {
        when (view.id) {
            R.id.searchPutStorageOrderBtn -> {
                activity.pageStoreIndex = 1
                activity.productID = ""
                activity.orderState = ""
                activity.orderCode = activity.searchPutStorageOrderET.text.toString().trim()
                getPutStorageList(activity.productID, activity.orderState, activity.orderCode, 1, 999)
            }
            R.id.putStorageStateLinear -> {
                PutStatePopup(activity, activity.putStorageProductNameLinear, OneYardUtils().getPutStorageState()).setListener(object : PutStatePopup.PutStateListener {
                    override fun handleResult(putState: PutState) {
                        putStateSelect = putState
                        activity.putStorageStateTV.text = putState.putStorageName
                        activity.putStorageProductNameTV.text = "产品名称"
                        activity.pageStoreIndex = 1
                        activity.productID = ""
                        activity.orderState = putState.putStorageId.toString()
                        activity.orderCode = ""
                        getPutStorageList(activity.productID, activity.orderState, activity.orderCode, 1, 999)
                    }
                })
            }
            R.id.putStorageProductNameLinear -> {
                HttpServer(this).getProductByProductNameList(ACacheUtil().getLoginInfo().EntID, "", 1, 999, activity)
            }
        }
    }

    fun getPutStorageList(ProductID: String, OrderState: String, OrderCode: String, pageStoreIndex: Int, pageSize: Int) {
        HttpServer(this).getStoreOrderList(ACacheUtil().getLoginInfo().EntID, pageStoreIndex, pageSize, ProductID, OrderState, OrderCode, activity)
    }


    fun initRecycler() {
        val recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.putStorageListRecycler)
        recyclerViewUtilKt.initRecyclerView()
        activity.putStorageListRecycler.useDefaultLoadMore() // 使用默认的加载更多的View。
        activity.putStorageListRecycler.setLoadMoreListener(mLoadMoreListener) // 加载更多的监听。


        putStorageRecordAdapter = PutStorageRecordAdapter(activity, R.layout.item_traceability_list, activity.storeOrderList!!)
        recyclerViewUtilKt.setAdapter(putStorageRecordAdapter!!)
        putStorageRecordAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, PutStorageRecordInfoActivity::class.java)
            intent.putExtra("storeOrderBean", activity.storeOrderList!![position])
            activity.startActivity(intent)
        }
    }


    private val mLoadMoreListener: SwipeMenuRecyclerView.LoadMoreListener = SwipeMenuRecyclerView.LoadMoreListener {
        activity.putStorageListRecycler.postDelayed({
            getPutStorageList(activity.productID, activity.orderState, activity.orderCode, activity.pageStoreIndex, activity.pageSize)
        }, 1000)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            27 -> {
                val resultData = data as ResultBean<MutableList<ProductNameBean>>
                if (resultData.State == "1") {
                    ProductNamePopup(activity, activity.putStorageProductNameLinear, resultData.ResultData).setListener(object : ProductNamePopup.ProductNameListener {
                        override fun handleResult(productNameBean: ProductNameBean) {
                            productNameSelect = productNameBean
                            activity.putStorageProductNameTV.text = productNameBean.ProductName
                            activity.putStorageStateTV.text = "入库状态"
                            activity.pageStoreIndex = 1
                            activity.productID = productNameBean.ProductID
                            activity.orderState = ""
                            activity.orderCode = ""
                            getPutStorageList(activity.productID, activity.orderState, activity.orderCode, 1, 999)
                        }
                    })
                } else {
                    ToastUtil.toast(activity, resultData.Message)
                }
            }
            11 -> {
                val storeOrderBeanList: MutableList<StoreOrderBean> = data as MutableList<StoreOrderBean>
                if (activity.pageStoreIndex == 1) {
                    if (storeOrderBeanList.size >= 0) {
                        activity.storeOrderList!!.removeAll(activity.storeOrderList!!)
                        activity.storeOrderList!!.addAll(storeOrderBeanList)
                        putStorageRecordAdapter?.notifyDataSetChanged()
                        activity.pageStoreIndex++
                        activity.putStorageListRecycler.loadMoreFinish(false, true)
                    }
                } else {
                    when {
                        storeOrderBeanList.size == 0 -> {
                            activity.putStorageListRecycler.loadMoreFinish(true, false)
                        }
                        storeOrderBeanList.size > 0 -> {
                            activity.pageStoreIndex++
                            activity.storeOrderList!!.addAll(storeOrderBeanList)
                            putStorageRecordAdapter?.notifyDataSetChanged()
                            activity.putStorageListRecycler.loadMoreFinish(false, true)
                        }
                        else -> activity.putStorageListRecycler.loadMoreError(0, "请求网络失败")
                    }
                }
            }
        }
    }
}